package com.youlai.boot.modules.game.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.modules.game.model.entity.GameCategory;
import com.youlai.boot.modules.game.model.form.GameCategoryForm;
import com.youlai.boot.modules.game.service.GameCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 游戏分类控制器
 *
 * @author 有来技术团队
 */
@Tag(name = "游戏分类接口")
@RestController
@RequestMapping("/api/v1/game/categories")
@RequiredArgsConstructor
public class GameCategoryController {

    private final GameCategoryService gameCategoryService;

    @Operation(summary = "获取分类分页列表")
    @GetMapping("/page")
    public PageResult<GameCategory> getCategoryPage(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页记录数") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "关键字") @RequestParam(required = false) String keywords
    ) {
        Page<GameCategory> page = gameCategoryService.getCategoryPage(pageNum, pageSize, keywords);
        return PageResult.success(page);
    }

    @Operation(summary = "获取所有分类")
    @GetMapping
    public Result<List<GameCategory>> getAllCategories() {
        List<GameCategory> list = gameCategoryService.getAllCategories();
        return Result.success(list);
    }

    @Operation(summary = "获取分类详情")
    @GetMapping("/{id}")
    public Result<GameCategory> getCategoryById(
            @Parameter(description = "分类ID") @PathVariable Long id
    ) {
        GameCategory category = gameCategoryService.getCategoryById(id);
        return Result.success(category);
    }

    @Operation(summary = "新增分类")
    @PostMapping
    public Result<Long> addCategory(
            @Validated @RequestBody GameCategoryForm formData
    ) {
        Long id = gameCategoryService.addCategory(formData);
        return Result.success(id);
    }

    @Operation(summary = "修改分类")
    @PutMapping("/{id}")
    public Result<Boolean> updateCategory(
            @Parameter(description = "分类ID") @PathVariable Long id,
            @Validated @RequestBody GameCategoryForm formData
    ) {
        boolean result = gameCategoryService.updateCategory(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除分类")
    @DeleteMapping("/{ids}")
    public Result<Boolean> deleteCategories(
            @Parameter(description = "分类ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());
        boolean result = gameCategoryService.deleteCategories(idList);
        return Result.judge(result);
    }
} 